package com.bawei.a2010douyin.ui.editMe;

import com.bawei.a2010douyin.base.mvp.BasePresenter;
import com.bawei.a2010douyin.base.rx.BaseFunc;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;

public class EditPresenter extends BasePresenter<EditMeContract.IditeMeView> implements EditMeContract.IditeMePresenter {
    private EditMeModel mEditMeModel;
    public EditPresenter() {
        mEditMeModel = new EditMeModel();
    }

    @Override
    public void updateIcon(MultipartBody.Part file) {
        mEditMeModel.updateIcon(file)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new BaseFunc<String>())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(String s) {
                        if(isAttach()){
                            getView().showUpdateIcon(s);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
